package ch.uzh.ifi.seal.ssqa.model.diagram.providers;

import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.ENamedElement;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.gmf.runtime.emf.type.core.ElementTypeRegistry;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.swt.graphics.Image;

import ch.uzh.ifi.seal.ssqa.model.ModelPackage;
import ch.uzh.ifi.seal.ssqa.model.diagram.edit.parts.DiagramComposite2EditPart;
import ch.uzh.ifi.seal.ssqa.model.diagram.edit.parts.DiagramCompositeEditPart;
import ch.uzh.ifi.seal.ssqa.model.diagram.edit.parts.DiagramEditPart;
import ch.uzh.ifi.seal.ssqa.model.diagram.edit.parts.DiagramFolder2EditPart;
import ch.uzh.ifi.seal.ssqa.model.diagram.edit.parts.DiagramFolderEditPart;
import ch.uzh.ifi.seal.ssqa.model.diagram.edit.parts.DiagramModule2EditPart;
import ch.uzh.ifi.seal.ssqa.model.diagram.edit.parts.DiagramModuleEditPart;
import ch.uzh.ifi.seal.ssqa.model.diagram.edit.parts.DiagramVComEditPart;
import ch.uzh.ifi.seal.ssqa.model.diagram.part.SsqaDiagramEditorPlugin;

/**
 * @generated
 */
public class SsqaElementTypes {

	/**
	 * @generated
	 */
	private SsqaElementTypes() {
	}

	/**
	 * @generated
	 */
	private static Map<IElementType, ENamedElement> elements;

	/**
	 * @generated
	 */
	private static ImageRegistry imageRegistry;

	/**
	 * @generated
	 */
	private static Set<IElementType> KNOWN_ELEMENT_TYPES;

	/**
	 * @generated
	 */
	public static final IElementType Diagram_1000 = getElementType("ch.uzh.ifi.seal.ssqa.diagram.Diagram_1000"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType DiagramFolder_2001 = getElementType("ch.uzh.ifi.seal.ssqa.diagram.DiagramFolder_2001"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType DiagramModule_2002 = getElementType("ch.uzh.ifi.seal.ssqa.diagram.DiagramModule_2002"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType DiagramComposite_2003 = getElementType("ch.uzh.ifi.seal.ssqa.diagram.DiagramComposite_2003"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType DiagramFolder_3001 = getElementType("ch.uzh.ifi.seal.ssqa.diagram.DiagramFolder_3001"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType DiagramModule_3002 = getElementType("ch.uzh.ifi.seal.ssqa.diagram.DiagramModule_3002"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType DiagramComposite_3003 = getElementType("ch.uzh.ifi.seal.ssqa.diagram.DiagramComposite_3003"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType DiagramVCom_4001 = getElementType("ch.uzh.ifi.seal.ssqa.diagram.DiagramVCom_4001"); //$NON-NLS-1$

	/**
	 * @generated
	 */
	private static ImageRegistry getImageRegistry() {
		if (imageRegistry == null) {
			imageRegistry = new ImageRegistry();
		}
		return imageRegistry;
	}

	/**
	 * @generated
	 */
	private static String getImageRegistryKey(ENamedElement element) {
		return element.getName();
	}

	/**
	 * @generated
	 */
	private static ImageDescriptor getProvidedImageDescriptor(
			ENamedElement element) {
		if (element instanceof EStructuralFeature) {
			EStructuralFeature feature = ((EStructuralFeature) element);
			EClass eContainingClass = feature.getEContainingClass();
			EClassifier eType = feature.getEType();
			if (eContainingClass != null && !eContainingClass.isAbstract()) {
				element = eContainingClass;
			} else if (eType instanceof EClass
					&& !((EClass) eType).isAbstract()) {
				element = eType;
			}
		}
		if (element instanceof EClass) {
			EClass eClass = (EClass) element;
			if (!eClass.isAbstract()) {
				return SsqaDiagramEditorPlugin.getInstance()
						.getItemImageDescriptor(
								eClass.getEPackage().getEFactoryInstance()
										.create(eClass));
			}
		}
		// TODO : support structural features
		return null;
	}

	/**
	 * @generated
	 */
	public static ImageDescriptor getImageDescriptor(ENamedElement element) {
		String key = getImageRegistryKey(element);
		ImageDescriptor imageDescriptor = getImageRegistry().getDescriptor(key);
		if (imageDescriptor == null) {
			imageDescriptor = getProvidedImageDescriptor(element);
			if (imageDescriptor == null) {
				imageDescriptor = ImageDescriptor.getMissingImageDescriptor();
			}
			getImageRegistry().put(key, imageDescriptor);
		}
		return imageDescriptor;
	}

	/**
	 * @generated
	 */
	public static Image getImage(ENamedElement element) {
		String key = getImageRegistryKey(element);
		Image image = getImageRegistry().get(key);
		if (image == null) {
			ImageDescriptor imageDescriptor = getProvidedImageDescriptor(element);
			if (imageDescriptor == null) {
				imageDescriptor = ImageDescriptor.getMissingImageDescriptor();
			}
			getImageRegistry().put(key, imageDescriptor);
			image = getImageRegistry().get(key);
		}
		return image;
	}

	/**
	 * @generated
	 */
	public static ImageDescriptor getImageDescriptor(IAdaptable hint) {
		ENamedElement element = getElement(hint);
		if (element == null) {
			return null;
		}
		return getImageDescriptor(element);
	}

	/**
	 * @generated
	 */
	public static Image getImage(IAdaptable hint) {
		ENamedElement element = getElement(hint);
		if (element == null) {
			return null;
		}
		return getImage(element);
	}

	/**
	 * Returns 'type' of the ecore object associated with the hint.
	 * 
	 * @generated
	 */
	public static ENamedElement getElement(IAdaptable hint) {
		Object type = hint.getAdapter(IElementType.class);
		if (elements == null) {
			elements = new IdentityHashMap<IElementType, ENamedElement>();

			elements.put(Diagram_1000, ModelPackage.eINSTANCE.getDiagram());

			elements.put(DiagramFolder_2001,
					ModelPackage.eINSTANCE.getDiagramFolder());

			elements.put(DiagramModule_2002,
					ModelPackage.eINSTANCE.getDiagramModule());

			elements.put(DiagramComposite_2003,
					ModelPackage.eINSTANCE.getDiagramComposite());

			elements.put(DiagramFolder_3001,
					ModelPackage.eINSTANCE.getDiagramFolder());

			elements.put(DiagramModule_3002,
					ModelPackage.eINSTANCE.getDiagramModule());

			elements.put(DiagramComposite_3003,
					ModelPackage.eINSTANCE.getDiagramComposite());

			elements.put(DiagramVCom_4001,
					ModelPackage.eINSTANCE.getDiagramVCom());
		}
		return (ENamedElement) elements.get(type);
	}

	/**
	 * @generated
	 */
	private static IElementType getElementType(String id) {
		return ElementTypeRegistry.getInstance().getType(id);
	}

	/**
	 * @generated
	 */
	public static boolean isKnownElementType(IElementType elementType) {
		if (KNOWN_ELEMENT_TYPES == null) {
			KNOWN_ELEMENT_TYPES = new HashSet<IElementType>();
			KNOWN_ELEMENT_TYPES.add(Diagram_1000);
			KNOWN_ELEMENT_TYPES.add(DiagramFolder_2001);
			KNOWN_ELEMENT_TYPES.add(DiagramModule_2002);
			KNOWN_ELEMENT_TYPES.add(DiagramComposite_2003);
			KNOWN_ELEMENT_TYPES.add(DiagramFolder_3001);
			KNOWN_ELEMENT_TYPES.add(DiagramModule_3002);
			KNOWN_ELEMENT_TYPES.add(DiagramComposite_3003);
			KNOWN_ELEMENT_TYPES.add(DiagramVCom_4001);
		}
		return KNOWN_ELEMENT_TYPES.contains(elementType);
	}

	/**
	 * @generated
	 */
	public static IElementType getElementType(int visualID) {
		switch (visualID) {
		case DiagramEditPart.VISUAL_ID:
			return Diagram_1000;
		case DiagramFolderEditPart.VISUAL_ID:
			return DiagramFolder_2001;
		case DiagramModuleEditPart.VISUAL_ID:
			return DiagramModule_2002;
		case DiagramCompositeEditPart.VISUAL_ID:
			return DiagramComposite_2003;
		case DiagramFolder2EditPart.VISUAL_ID:
			return DiagramFolder_3001;
		case DiagramModule2EditPart.VISUAL_ID:
			return DiagramModule_3002;
		case DiagramComposite2EditPart.VISUAL_ID:
			return DiagramComposite_3003;
		case DiagramVComEditPart.VISUAL_ID:
			return DiagramVCom_4001;
		}
		return null;
	}

}
